﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Transportr
{
    public class FileStorage
    {
        private static string storagePath;
        public static bool Valid { get; private set; }
        public static int FileAccessCounter { get; private set; }

        private static string getPath(string path)
        {
            return storagePath + path;
        }

        public static void Initialize()
        {
            if (FlashCardUtils.FlashCardExists())
            {
                storagePath = String.Format("\\{0}\\Application Data\\pozr\\", FlashCardUtils.GetFirstFlashCardPath());
            }
            else
            {
                storagePath = "\\Application Data\\pozr\\";
            }
            Valid = true;
        }

        #region File Manipulation

        public static FileStream GetFileStream(string path, FileMode mode)
        {
            if (Valid && (File.Exists(getPath(path)) || mode == FileMode.Create))
            {
                FileAccessCounter++;
                return File.Open(getPath(path), mode);
            }
            return null;
        }

        public static bool FileExists(string path)
        {
            return Valid && File.Exists(getPath(path));
        }

        public static bool DeleteFile(string path)
        {
            if (Valid && File.Exists(getPath(path)))
            {
                File.Delete(getPath(path));
                return true;
            }
            return false;
        }

        #endregion

        # region Directory manipulation

        public static bool DirectoryExists(string path)
        {
            return Valid && Directory.Exists(getPath(path));
        }

        public static void CreateDirectory(string path)
        {
            if (Valid)
            {
                Directory.CreateDirectory(getPath(path));
            }
        }

        public static void DeleteDirectory(string path)
        {
            if (Valid)
            {
                Directory.Delete(getPath(path));
            }
        }

        #endregion
    }

}
